package com.ogawa.joypal.user.util;

import android.content.Context;
import android.content.res.Resources;


import com.ogawa.joypal.user.R;
import com.ogawa.joypal.user.widget.NumberPickerView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 时间选择器的工具类
 * Created by HourGlassRemember on 2018/11/12.
 */
public class TimeUtil {

    public static final String FORMAT_DATE_1 = "MM-dd, HH:mm";
    public static final String FORMAT_DATE_2 = "HH:mm";
    public static final String FORMAT_DATE_3 = "MM.dd";
    public static final String FORMAT_DATE_4 = "YYYY年MM月dd日";
    public static final String FORMAT_DATE_5 = "YYYYMMddHHmm";

    private Context mContext;// 上下文

    /**
     * 构造函数
     *
     * @param mContext 上下文
     */
    public TimeUtil(Context mContext) {
        this.mContext = mContext;
    }

    /**
     * 底部弹出带数字的弹框
     *
     * @param pickerView   时间选择器
     * @param array        显示的数据
     * @param unitId       单位，当值为 -1 的时候表示没有单位
     * @param maxValue     最大值的下标
     * @param defaultValue 默认值的下标
     */
    public void setData(NumberPickerView pickerView, String[] array, int unitId, int maxValue, int defaultValue) {
        Resources resources = mContext.getResources();
        if (-1 != unitId) {
            pickerView.setHintText(resources.getString(unitId));
        }
        pickerView.setDisplayedValues(array);
        pickerView.setMinValue(0);// 设置最小值
        pickerView.setMaxValue(maxValue);// 设置最大值
        pickerView.setValue(defaultValue);// 设置默认值
    }

    /**
     * 获取当前月份多少天
     *
     * @param year  年
     * @param month 月
     */
    public int getNewDay(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);// java 月份从0开始算
        return calendar.getActualMaximum(Calendar.DATE);
    }

    /**
     * 改变天数的数据源
     *
     * @param pickerView 时间选择器
     * @param year       年
     * @return
     */
    public String[] changeMonth(NumberPickerView pickerView, int year) {
        String[] monthStr;
        if (year == Calendar.getInstance().get(Calendar.YEAR)) {
            int currentMoth = Calendar.getInstance().get(Calendar.MONTH);
            monthStr = new String[currentMoth + 1];
            for (int i = 0; i < currentMoth + 1; i++) {
                monthStr[i] = String.valueOf(i + 1);
            }
        } else {
            monthStr = mContext.getResources().getStringArray(R.array.array_month);
        }
        if (monthStr.length != pickerView.getRawContentSize()) {
            pickerView.refreshByNewDisplayedValues(monthStr);
        }
        return monthStr;
    }

    /**
     * 改变天数的数据源
     *
     * @param pickerView 时间选择器
     * @param year       年
     * @param month      月
     * @return
     */
    public String[] changeDay(NumberPickerView pickerView, int year, int month) {
        String[] dayStr = new String[0];
        if (year == Calendar.getInstance().get(Calendar.YEAR)
                && month == (Calendar.getInstance().get(Calendar.MONTH) + 1)) {
            int currentDate = Calendar.getInstance().get(Calendar.DATE);
            dayStr = new String[currentDate];
            for (int i = 0; i < currentDate; i++) {
                dayStr[i] = String.valueOf(i + 1);
            }
            if (dayStr.length != pickerView.getRawContentSize()) {
                pickerView.refreshByNewDisplayedValues(dayStr);
            }
            return dayStr;
        }
        switch (getNewDay(year, month)) {
            case 31:
                dayStr = mContext.getResources().getStringArray(R.array.array_day_31);
                break;
            case 30:
                dayStr = mContext.getResources().getStringArray(R.array.array_day_30);
                break;
            case 29:
                dayStr = mContext.getResources().getStringArray(R.array.array_day_29);
                break;
            case 28:
                dayStr = mContext.getResources().getStringArray(R.array.array_day_28);
                break;
            default:
                break;
        }
        if (getNewDay(year, month) != pickerView.getRawContentSize()) {
            pickerView.refreshByNewDisplayedValues(dayStr);
        }
        return dayStr;
    }

    /**
     * 根据某种时间格式获取当前时间
     *
     * @param pattern 时间格式
     * @return
     */
    public static String getCurrentTime(String pattern) {
        return new SimpleDateFormat(pattern, Locale.CHINA).format(new Date(System.currentTimeMillis()));
    }

    /**
     * 根据时间戳和某种时间格式获取时间
     *
     * @param timeMillis 时间戳
     * @param pattern    时间格式
     * @return
     */
    public String getTimeByTimeMillis(long timeMillis, String pattern) {
        return new SimpleDateFormat(pattern, Locale.CHINA).format(new Date(timeMillis));
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(long s, String patter) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(patter);
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }

}
